package com.wmx.datastructuresandalgorithms;

public class MyQueue {
   private Object [] queueArray;
   //队列总大小
   private int  maxSize;
   //前端
   private int  front;
   // 后端
   private int  rear;
    //队列中元素的实际数目
   private int nItems;

   MyQueue(int size){
       this.maxSize = size;
       this.queueArray = new Object[maxSize];
       this.front = 0;
       this.rear = -1;
       this.nItems = 0;
   }


   //判断队列是否满

    public boolean isFull(){
       return (nItems == maxSize);
    }

    //查看头队列
    public Object peekFront(){
        return  queueArray[front];
    }

    //查看尾队列
    public Object peekRear(){
        return  queueArray[rear];
    }

    //插入数据
    public void  inster(Object obj){
        if(isFull()){
            throw  new IllegalArgumentException("队列已满"+obj);
        }else {
            if(rear==maxSize-1){
                rear = -1;
            }

            queueArray[++rear] = obj;
            nItems++;
        }
    }

    //删除队列
    public Object remove(){
        Object removeValue=null;

        if(nItems==0)
            throw  new IllegalArgumentException("队列中没有元素");

        removeValue = queueArray[front];
        queueArray[front] = null;
        front++;
        if(front==maxSize){
            front = 0;
        }
        nItems--;
        return removeValue;
    }


    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue(3);
        myQueue.inster(2);
        myQueue.inster(3);
        myQueue.inster(4);
        //myQueue.inster(5);
        System.out.printf("队列中头元素======"+myQueue.peekFront());
        myQueue.remove();
        myQueue.inster(9);
        System.out.printf("队列中头元素======"+myQueue.peekFront());
        System.out.printf("队列中尾元素======"+myQueue.peekRear());

    }

}
